package com.example.system.service.impl.dubbo;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.commons.core.entity.RoleMenuRelation;
import com.example.system.service.RoleMenuRelationService;
import com.example.system.api.service.SystemMenuDubboService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@DubboService
@Component
public class SystemMenuDubboServiceImpl implements SystemMenuDubboService {
    private final RoleMenuRelationService roleMenuRelationService;

    public SystemMenuDubboServiceImpl(RoleMenuRelationService roleMenuRelationService) {
        this.roleMenuRelationService = roleMenuRelationService;
    }

    @Override
    public void addOrUpdateRoleMenuRelation(List<String> menuIds, Long roleId) {
        // 关联表先删除，后添加
        roleMenuRelationService.remove(new LambdaQueryWrapper<RoleMenuRelation>().eq(RoleMenuRelation::getRoleId, roleId));
        List<RoleMenuRelation> roleMenuRelations = menuIds.stream().map(x -> new RoleMenuRelation(roleId, Long.parseLong(x), LocalDateTime.now())).collect(Collectors.toList());
        roleMenuRelationService.saveBatch(roleMenuRelations);
    }
}
